<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload_images extends Admin_Controller{

	function Upload_images(){
        parent::Admin_Controller();
        $CI = & get_instance();    
	}
	
	function index(){
		$this->lists();
	}
    
    function lists(){		
        $CI = & get_instance();
        $base_url = 'admin/upload_images.upload_images.lists';
		$CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
		$CI->load->library("fckeditor");
		$path = $CI->fckeditor->UploadPath.'/images';

        $newpath = rtrim($_SERVER['DOCUMENT_ROOT'], "/").$path;

        $fileattrib = array();
		if(!@is_dir($newpath)){
            $wd = '/';
            $dir_stack = explode($wd, $path);
            $tmp_path = rtrim($_SERVER['DOCUMENT_ROOT'], $wd).$wd;
            $tmp_path = $tmp_path.array_shift($dir_stack);
            while(!empty($dir_stack)){
                $tmp_path = $tmp_path.$wd.array_shift($dir_stack);
                if(!@is_dir($tmp_path))
                    mkdir( rtrim($tmp_path, '/'), 0777 ) ;
            }
        }
        if ($dir = @opendir($newpath)) {		
			// loop once for each name in the directory 
			$fc=0;
			while($file = readdir($dir)) {
				$match="1";
				// if the name is not a directory and the name is not the name of this program file
				if($file != "." && $file != "..") {
					$match = 0;
				}
				
				if (!$this->_hidecheck($file)) { $match=1; }        
				// if there were no matches the file should not be hidden
				if(!$match) {
					$filedata = stat($newpath.'/'.$file); // get some info about the file
					$fileattrib[$fc][0] = $file;
					$fileattrib[$fc][1] = $this->_setsize($filedata[7]); // size in bytes
					$fileattrib[$fc][2] = $filedata[9]; // time of last modification
					$fileattrib[$fc][5] = date("m/d/Y h:i:sA",$filedata[9]);
					/*
					if (is_dir($newpath.'/'.$file)) { $fileattrib[$fc][3]="Folder"; }
					else { 
						//$ft=getfiletype($file);
						$fileattrib[$fc][3]=$ft[0]; // TYPE
						$fileattrib[$fc][4]=$ft[1]; // Download or Browse 
					} 
					*/
					$fc++;
				}
				
			}
		}

		/*
		else {
	  
			$patherror="<p><center><font face=\"Verdana, Arial, Hevetica\" size=\"3\" color=red><b>directory incorrectly defined</b></font></center></p>";
		}
		*/
		$this->add("fileattrib",$fileattrib);
		$this->add("path",$path);
        $this->module_toolbar('upload_images.toolbar');
		$this->module_navigation(lang('_title'));
        $this->view('lists');
    }
	
	function _setsize($size) {

		// Setup some common file size measurements.
		$kb = 1024;         // Kilobyte
		$mb = 1024 * $kb;   // Megabyte
		$gb = 1024 * $mb;   // Gigabyte
		$tb = 1024 * $gb;   // Terabyte
		
		/* If it's less than a kb we just return the size, otherwise we keep going until
		the size is in the appropriate measurement range. */
		if($size < $kb) {
			return $size." B";
		}
		else if($size < $mb) {
			return round($size/$kb,2)." KB";
		}
		else if($size < $gb) {
			return round($size/$mb,2)." MB";
		}
		else if($size < $tb) {
			return round($size/$gb,2)." GB";
		}
		else {
			return round($size/$tb,2)." TB";
		}
	}
	function _hidecheck ($ckfilename) {
		$HiddenFiles = array(".svn","Thumbs.db"); 
		$okay=1;
		foreach($HiddenFiles as $name) {
			// check the name is not the same as the hidden file name
			if($ckfilename == $name) {  
				$okay = "";  // unset if not okay
			}
		}   
		return $okay;
	}
   
  
}